package com.falcon.generator.java;

import com.falcon.config.GeneratorConfig;
import com.github.pagehelper.util.StringUtil;
import com.swan.freemarker.core.IFreemarkerTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.io.File;

/**
 * @author zongf
 * @since 2023-05-18
 **/
@Slf4j
public abstract class JavaAbsGenerator implements IJavaGenerator {

    private String projectPath = null;

    @Autowired
    protected GeneratorConfig config;

    @Autowired
    @Qualifier("freemarkerTemplateInside")
    protected IFreemarkerTemplate freemarkerTemplateInside;

    protected String getName(String prefix, String name, String suffix) {
        StringBuffer sb = new StringBuffer();
        if (StringUtil.isNotEmpty(prefix)) {
            sb.append(prefix);
        }
        sb.append(name);
        if (StringUtil.isNotEmpty(suffix)) {
            sb.append(suffix);
        }
        return sb.toString();
    }

    /** 项目根目录  */
    public synchronized String geGenerateCodePath() {
        if (projectPath == null) {
            if (StringUtils.isNotEmpty(this.config.getProjectPath())) {
                projectPath = this.config.getProjectPath();
            } else {
                File file = new File("tmp");
                projectPath = StringUtils.substringBeforeLast(file.getAbsolutePath(), "/");
                projectPath += "/services";
            }
        }
        return projectPath;
    }


}
